﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConfigGen.Core.ConfigurationSettings
{
    /// <summary>
    /// Represents a configuration settings for multiple machines. Each machine has a single <see cref="MachineConfigurationSettings"/> entry. This class is immutable.
    /// </summary>
    [Serializable]
    public class MachinesConfigurationCollection
    {
        private readonly MachineConfigurationSettings[] _machineConfigurationSettings;

        /// <summary>
        /// Initializes a new instance of the <see cref="MachinesConfigurationCollection"/> class.
        /// </summary>
        /// <param name="machineSettings">The machine settings.</param>
        public MachinesConfigurationCollection(IEnumerable<MachineConfigurationSettings> machineSettings)
        {
            _machineConfigurationSettings =
                machineSettings.Select(
                    machine =>
                    new MachineConfigurationSettings(machine.MachineName, machine.ConfigFilePath,
                                                     machine.ConfigurationSettings, this)).ToArray();
        }

        /// <summary>
        /// Gets the list of machine configuration settings represented by this class.
        /// </summary>
        /// <value>The machine settings.</value>
        public IEnumerable<MachineConfigurationSettings> MachineConfigurationSettings
        {
            get { return (IEnumerable<MachineConfigurationSettings>)_machineConfigurationSettings.Clone(); }
        }

        /// <summary>
        /// Gets the machine configuration settings instance (being the representation of all config settings for a single machine)
        /// matching the specified machine name. Note machine name is case insensitive.
        /// </summary>
        /// <param name="machineName">Machine name (case insensitive)</param>
        /// <returns>Matching machine configuration settings instance, or null if no matches.</returns>
        public MachineConfigurationSettings this[string machineName]
        {
            get { return GetMachineConfigurationSettings(machineName);}
            
        }

        /// <summary>
        /// Gets the machine configuration settings instance (being the representation of all config settings for a single machine)
        /// matching the specified machine name. Note machine name is case insensitive.
        /// </summary>
        /// <param name="machineName">Machine name (case insensitive)</param>
        /// <returns>Matching machine configuration settings instance, or null if no matches.</returns>
        public MachineConfigurationSettings GetMachineConfigurationSettings(string machineName)
        {
            var machineSettings = GetMachineConfigurationSettingsInternal(machineName);

            if (machineSettings == null)
            {
                return null;
            }

            return new MachineConfigurationSettings(
                machineSettings.MachineName,
                machineSettings.ConfigFilePath,
                machineSettings.ConfigurationSettings,
                this);
        }

        /// <summary>
        /// Returns true if this instance contains a machine configuration settings instance for the specified machine name, otherwise false.
        /// </summary>
        /// <param name="machineName">Machine name</param>
        /// <returns>True if a match exists, otherwise false.</returns>
        public bool Contains(string machineName)
        {
            return GetMachineConfigurationSettingsInternal(machineName) != null;
        }

        private MachineConfigurationSettings GetMachineConfigurationSettingsInternal(string machineName)
        {
            return _machineConfigurationSettings.FirstOrDefault(m => string.Equals(m.MachineName, machineName, StringComparison.OrdinalIgnoreCase));

        }
    }
}
